#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include"logger.h"
#include "sqlite3.h"
#include <errno.h>


void sqlite_create(sqlite3 *db){

        char            *sql;
        char            *zErrMsg;
        int             rc=-1;

        sql = "CREATE TABLE IF NOT EXISTS temper_discon(" \
    "ID INTEGER   PRIMARY KEY   AUTOINCREMENT," \
    "NAME        CHAR(50)      NOT NULL,"  \
    "TIME     CHAR(50)      NOT NULL,"  \
    "TEMPERATURE  CHAR(10)      NOT NULL);" ;


        rc=sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);

        if(rc!=SQLITE_OK)
        {
                printf("sqlite error:%s\n",strerror(errno));

        }
        else
        {
                printf("table create successful\n");
        }
}

void sqlite_insert(sqlite3 *db,char *buf){
        char            *zErrMsg;
        int             rc=-1;
        char            sql[128];
        char            *name;
        char            *T_time;
        char            *temperature;

        name=strtok(buf,"/");
        T_time=strtok(NULL,"/");
        temperature=strtok(NULL,"/");
        snprintf(sql, sizeof(sql), "INSERT INTO temper_discon VALUES(NULL, '%s', '%s', '%s');", name, T_time, temperature);
        rc=sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
        if(rc!=SQLITE_OK){
                printf("sql insert error:%s\n",strerror(errno));                        }
        else{
                printf("sql insert successful\n");
        }
}


int sqlite_delete(sqlite3 *db){
        char            *zErrMsg;
        int             rc=-1;
        char            *sql="DELETE from temper_discon where NAME=0001 ;"\
        "select * from temper_discon; ";
         rc = sqlite3_exec(db, sql,NULL,NULL, &zErrMsg);
         if( rc != SQLITE_OK ){
                 printf( "delete table data  error: %s\n", zErrMsg);
         }else{
                 printf("delete table data successfully\n");
                 sqlite3_free(zErrMsg);
         }
         return 0;
}


//int getfieldvalue(char *fieldname, char *fieldvalue, char *condfield, char *condvalue, char *table)


int sqlite_get(sqlite3 *db,char *data){

        char            *zErrMsg;
        int             rc=-1;
        char            sql[64];
        int             j=0;
        int             index=0;
        int             nrow=0;
        char            buf[64];
        char             **table_data;

        memset(buf,0,sizeof(buf));
        snprintf(sql,sizeof(sql),"select * from %s limit 1;","temper_discon");

        rc=sqlite3_get_table(db,sql,&table_data,&nrow,NULL,&zErrMsg);
        if(rc!=SQLITE_OK){
                printf("rc=%d\n",rc);
                printf("sqlite3_get_table failure:%s\n",strerror(errno));
                sqlite3_free(zErrMsg);
                return -1;
        }

        if(0==nrow){
                sqlite3_free_table(table_data);
                printf("no data,free table\n");
        }
        else
        {
                memset(data,0,sizeof(data));
                snprintf(data,64,"%s/%s/%s",table_data[5],table_data[6],table_data[7]);
                printf("get data\n");

                sqlite3_free_table(table_data);
        }
                return -1;
}

